<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Authority_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $user_id
     * @return mixed
     * 查询单条
     */
    public function get_users_authority_by_id($user_id){

        $this->db->select('*');
        $this->db->from('users_authority');
        $this->db->where('user_id',$user_id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $user_id
     * @param $data
     * @return bool
     * 修改商品信息
     */
    public function sava_users_authority($user_id,$data){

        $this->db->where('user_id',$user_id);
        $this->db->update('users_authority', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增商品
     */
    public function add_users_authority($data){

        $this->db->insert('users_authority',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有商品
     */
    public function get_all_users_authority($start,$limit,$uid=""){
        $this->db->select('*');
        $this->db->from('users_authority');
        if(!empty($uid)){
            $this->db->where('uid',$uid);
        }
        $this->db->limit($limit,$start)->order_by('user_id','DESC');
        $this->db->order_by('user_id','DESC');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_users_authority_by($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}users_authority  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
}
